import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Register from '../views/Register.vue'
import Login from '../views/Login.vue'
import RoomList from '../views/RoomList.vue'
import SeatList from '../views/SeatList.vue'
import DishList from '../views/DishList.vue'
import MemberCenter from '../views/MemberCenter.vue'
import About from '../views/About.vue'
import FoodCart from '../views/FoodCart.vue'
import Contact from '../views/Contact.vue'
import AdminDashboard from '../views/admin/content/AdminDashboard.vue'
import Admin from '../views/admin/index.vue'
import AdminNews from '../views/admin/content/AdminNews.vue'
import AdminMembers from '../views/admin/content/AdminMembers.vue'
import AdminRooms from '../views/admin/content/AdminRooms.vue'
import AdminSeats from '../views/admin/content/AdminSeats.vue'
import AdminDishes from '../views/admin/content/AdminDishes.vue'
import AdminDishCategory from'../views/admin/content/AdminDishCategory.vue'
import AdminRoomOrders from '../views/admin/content/AdminRoomOrders.vue'
import AdminSeatOrders from '../views/admin/content/AdminSeatOrders.vue'
import AdminDishOrders from '../views/admin/content/AdminDishOrders.vue'
import AdminDishOrderItem from '../views/admin/content/AdminDishOrderItem.vue'
import AdminMessages from '../views/admin/content/AdminMessages.vue'
import AdminComments from '../views/admin/content/AdminComments.vue'
import NotFound from '../views/NotFound.vue'
import upload from '../views/upload.vue'

import AdminDishCart from '../views/admin/content/AdminDishCart.vue'
import AdminCollection from '../views/admin/content/AdminCollection.vue'
import AdminWebsiteInfo from '../views/admin/content/AdminWebsiteInfo.vue'
import { getToken , getAdmin} from '@/utils/auth'
Vue.use(VueRouter)



const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => {
    if (err.name !== 'NavigationDuplicated') throw err
  })
}

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/home',
    name: 'Home',
    component: Home
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/rooms',
    name: 'RoomList',
    component: RoomList
  },
  {
    path: '/seats',
    name: 'SeatList',
    component: SeatList
  },
  {
    path: '/dishes',
    name: 'DishList',
    component: DishList
  },
  {
    path: '/member',
    name: 'MemberCenter',
    component: MemberCenter,
     meta: { requiresAuth: true }
  },
  {
    path: '/about',
    name: 'About',
    component: About
  },
  {
    path: '/FoodCart',
    name: 'FoodCart',
    component: FoodCart
  },
  {
    path: '/Contact',
    name: 'Contact',
    component: Contact
  },
  {
    path: '/admin',
    name: 'admin',
    component:Admin,
     meta: { requiresAdmin: true },
    children:[
      {
        path: '/admin/Dashboard',
        name: 'AdminDashboard',
        component: AdminDashboard,
        //meta: { requiresAdmin: true }
        meta: { title: '欢迎访问酒店后台管理系统' }
      },
      {
        path: '/admin/news',
        name: 'AdminNews',
        component: AdminNews,
        // meta: { requiresAdmin: true }
        meta: { title: '欢迎使用新闻管理模块！' }
      },
      {
        path: '/admin/DishCart',
        name: 'AdminDishCart',
        component: AdminDishCart,
        // meta: { requiresAdmin: true }
        meta: { title: '欢迎使用购物车管理模块！' }
      },
      {
        path: '/admin/Collection',
        name: 'AdminCollection',
        component: AdminCollection,
        // meta: { requiresAdmin: true }
        meta: { title: '欢迎使用收藏管理模块！' }
      },
      {
        path: '/admin/WebsiteInfo',
        name: 'AdminWebsiteInfo',
        component: AdminWebsiteInfo,
        // meta: { requiresAdmin: true }
        meta: { title: '欢迎使用网站信息管理模块！' }
      },
      {
        path: '/admin/members',
        name: 'AdminMembers',
        component: AdminMembers,
        meta: { title: '欢迎使用会员管理模块！' }
        //meta: { requiresAdmin: true }
      },
      {
        path: '/admin/rooms',
        name: 'AdminRooms',
        component: AdminRooms,
        meta: { title: '欢迎使用房间管理模块！' }
        //meta: { requiresAdmin: true }
      },
      {
        path: '/admin/seats',
        name: 'AdminSeats',
        component: AdminSeats,
        meta: { title: '欢迎使用座位管理模块！' }
        //meta: { requiresAdmin: true }
      },
      {
        path: '/admin/dishes',
        name: 'AdminDishes',
        component: AdminDishes,
        meta: { title: '欢迎使用菜品管理模块！' }
        //meta: { requiresAdmin: true }
      },
      {
        path: '/admin/DishCategory',
        name: 'AdminDishCategory',
        component: AdminDishCategory,
        meta: { title: '欢迎使用菜品分类管理模块！' }
        //meta: { requiresAdmin: true }
      },
      {
        path: '/admin/room-orders',
        name: 'AdminRoomOrders',
        component: AdminRoomOrders,
        meta: { title: '欢迎使用房间订单管理模块！' }
        //meta: { requiresAdmin: true }
      },
      {
        path: '/admin/seat-orders',
        name: 'AdminSeatOrders',
        component: AdminSeatOrders,
        meta: { title: '欢迎使用座位订单管理模块！' }
        //meta: { requiresAdmin: true }
      },
      {
        path: '/admin/dish-orders',
        name: 'AdminDishOrders',
        component: AdminDishOrders,
        meta: { title: '欢迎使用菜品订单管理模块！' }
        //meta: { requiresAdmin: true }
      },
      {
        path: '/admin/DishOrderItem',
        name: 'AdminDishOrderItem',
        component: AdminDishOrderItem,
        meta: { title: '欢迎使用菜品订单详情管理模块！' }
        //meta: { requiresAdmin: true }
      },
      {
        path: '/admin/messages',
        name: 'AdminMessages',
        component: AdminMessages,
        meta: { title: '欢迎使用留言管理模块！' }
        //meta: { requiresAdmin: true }
      },
      {
        path: '/admin/comments',
        name: 'AdminComments',
        component: AdminComments,
        meta: { title: '欢迎使用评论管理模块！' }
        //meta: { requiresAdmin: true }
      }
    ]
  },
  {
    path: '/upload',
    name: 'upload',
    component: upload
  },
  // 404页面路由配置 - 必须放在所有路由的最后
  {
    path: '*',
    name: 'NotFound',
    component: NotFound
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// router.beforeEach((to, from, next) => {
//   const isAuthenticated = getToken();
//   const isAdmin = getAdmin() === 'true'

//   if (to.meta.requiresAuth && !isAuthenticated) {
//     next({ name: 'Login' })
//   } else if (to.meta.requiresAdmin && (!isAuthenticated || !isAdmin)) {
//     next({ name: 'Home' })
//   } else {
//     next()
//   }
// })
router.beforeEach((to, from, next) => {
  const isAuthenticated = getToken();
  const isAdmin = getAdmin() === 'true';
  const { requiresAuth, requiresAdmin } = to.meta;

  // 未登录用户访问需要认证的页面
  if (requiresAuth && !isAuthenticated) {
    return next({ name: 'Login' });
  }

  // 非管理员用户访问管理页面
  if (requiresAdmin && !isAdmin) {
    // 使用Vue的消息提示组件（需确保项目中已引入）
    Vue.prototype.$message?.error('抱歉，您没有权限访问该页面');
    return next(from.fullPath); // 停留在当前页面
  }

  next();
});

export default router    